package com.ybwh.springboot2.sharding5;

import com.ybwh.springboot2.sharding5.dao.ReportDao;
import com.ybwh.springboot2.sharding5.model.po.Report;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * 按月分表例子
 *
 */
@SpringBootTest
public class TesShardingTableByMonth {

	@Resource
	private ReportDao dao;

	@Test
	public void testInsert() throws ParseException {
		assertNotNull(dao);
		HintManager hintManager = HintManager.getInstance();
		hintManager.setWriteRouteOnly();
		hintManager.close();
		
		DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Report report = new Report();
		report.setCreateTime(new Date());
		report.setSaleCount(555);
		report.setSaleAmount(new BigDecimal("4545788"));

		try {
			dao.saveIgnoreNull(report);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testSelect() {
		assertNotNull(dao);

		/**
		 * 单列分表只要条件中带有分表列就可以查询成功，否则无法查询成功
		 */
		try {

			DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//			List<Report> list = dao.selectEqCreateTime(dFormat.parse("2018-09-17 16:02:01"));
//			System.out.println(list);

//			List<Report> list1 = dao.selectInCreateTime(Arrays.asList(dFormat.parse("2018-09-17 16:02:01")));
//			System.out.println(list1);
			
//			List<Report> list2 = dao.selectBetweenCreateTime(dFormat.parse("2018-08-17 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
//			System.out.println(list2);
			
			
			Integer count = dao.selectCount(dFormat.parse("2018-08-16 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
			System.out.println(count);
			
			long sum = dao.selectSum(dFormat.parse("2018-08-17 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
			System.out.println(sum);
			
			
			System.out.println(
					"----------------------------------------------------------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
